// pages/interpretreport/interpretreport.ts
import api from "../../utils/api"
import animationHelper from '../..//utils/animation';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    progress: 1 as number,//1、未申请解读，2、等待解读，3、解读完成
    id: 0 as number,
    phone: '' as string,
    name: '' as string,
    notes: '' as string,
    explainInfo: {} as ReportModel,
    showTime: false as boolean,//展示时间弹窗
    animation: null as WechatMiniprogram.Animation | null,
    animationData: {} as any,
    time: [] as string[],//picker遍历的数组
    beginTime: '' as string,
    endTime: '' as string,
    timeValue: "" as string,//页面展示的时间
    value: [0, 0] as number[],
    tempValue: [] as number[],
    canClose: true as boolean,//是否可以关闭
    currentDate: '' as string,//当前日期
    visible: false as boolean,// 是否展示日期弹窗
    showDate: '' as string,// 展示的日期
  },
  // 申请解读
  async applyInterpret(): Promise<void> {
    let mobilePattern = /^1[3-9]\d{9}$/;
    const { id, name, phone, notes, showDate, timeValue, beginTime, endTime } = this.data
    if (!name || !phone || !showDate || !timeValue) {
      wx.showToast({
        title: "存在为空的项",
        icon: "error"
      })
      return
    }
    if (!mobilePattern.test(phone)) {
      wx.showToast({
        title: "手机号格式错误",
        icon: "error"
      })
      return
    }
    const appointmentTime = showDate + " " + beginTime + ' ~ ' + showDate + " " + endTime
    const params = {
      id,
      name,
      phone,
      appointmentTime,
      remark: notes
    }
    console.log(params)
    const res = await api.report.applyReportExplain(params)
    if (res.data.code == "000") {
      wx.showToast({
        title: "申请成功",
        success: () => {
          wx.navigateBack()
        }
      })
    } else {
      wx.showToast({
        title: res.data.message as string,
        icon: "error"
      })
    }
  },
  // 输入姓名
  handleName(e: any): void {
    this.setData({
      name: e.detail.value
    })
  },
  handlePhone(e: any): void {
    this.setData({
      phone: e.detail.value
    })
  },
  handleNotes(e: any): void {
    this.setData({
      notes: e.detail.value
    })
  },
  back(): void {
    wx.navigateBack()
  },
  closeDialog(): void {
    const animation = this.data.animation;
    animationHelper.closeAnimation(animation, 450, this.setData.bind(this), "showTime");
  },
  showDialog(): void {
    if (this.data.progress == 2) {
      return
    }
    const animation = animationHelper.createAnimation(500, 'ease');
    this.setData({ animation: animation });
    animationHelper.showAnimation(animation, 450, this.setData.bind(this), "showTime");
  },
  bindChange(e: any): void {
    const { time } = this.data
    const beginIndex = e.detail.value[0];
    const endIndex = e.detail.value[1];
    this.setData({
      beginTime: time[beginIndex],
      endTime: time[endIndex],
      tempValue: e.detail.value
    })
  },
  // 开始选择
  pickerStart(): void {
    this.setData({
      canClose: false
    })
  },
  pickerEnd(): void {
    this.setData({
      canClose: true
    })
  },
  confirm(): void {
    const { beginTime, endTime, tempValue } = this.data
    if (!this.data.canClose) {
      wx.showToast({
        title: "请稍后点击",
        icon: "error"
      })
      return
    }
    if (tempValue[0] > tempValue[1]) {
      wx.showToast({
        title: "请选择合适时间",
        icon: "error"
      })
      return
    }
    this.setData({
      timeValue: beginTime + " ~ " + endTime,
      value: tempValue,
    })
    this.closeDialog()
  },
  showCalendar(): void {
    if (this.data.progress == 2) {
      return
    }
    this.setData({
      visible: true
    })
  },
  // 关闭弹窗
  close(): void {
    this.setData({
      visible: false
    })
  },
  getRange(e: any): void {
    console.log(e)
    this.setData({
      showDate: e.detail.time
    })
    this.close();
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options: any) {
    try {
      if (options.id) {
        const explainInfo = JSON.parse(options.info)
        console.log(explainInfo)
        if (explainInfo.explainAppointmentTime && options.status == "APPLIED") {
          let [startDateTime, endDateTime] = explainInfo.explainAppointmentTime.split('~');
          let [showDate, beginTime] = startDateTime.trim().split(' ');
          let endTime = endDateTime.trim().split(' ')[1];
          let newTimeRange = beginTime + " ~ " + endTime;
          explainInfo.explainAppointmentTime = newTimeRange
          this.setData({
            showDate: showDate
          })
        }
        this.setData({
          id: Number(options.id),
          progress: options.status == 'EXPLAINED' ? 3 : options.status == 'APPLIED' ? 2 : 1,
          explainInfo
        })
      }
      const date = new Date();
      const year = date.getFullYear();
      const month = (date.getMonth() + 1).toString().padStart(2, "0");
      const day = date.getDate().toString().padStart(2, "0");
      const currentDate = year + '-' + month + '-' + day;
      const time = [];
      for (let i = 0; i <= 24; i++) {
        let hour = i < 10 ? '0' + i : i;
        time.push(hour + ":00:00");
      }
      this.setData({
        time,
        beginTime: time[0],
        endTime: time[0],
        currentDate
      })
    } catch (error) {
      console.error("JSON.parse error")
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})